using System;
using System.Collections.Generic;
using System.Text;

namespace Wrath.Xml.Objects
{
    public class Script : IElementContainer
    {
        private string file;
        private string inline;

        public string File
        {
            get { return file; }
            set { file = value; }
        }

        public string Inline
        {
            get { return inline; }
            set { inline = value; }
        }

        public Script(Element element)
        {
            sourceElement = element;

            Attribute fileAttribute = sourceElement.FindAttribute("file");
            if (fileAttribute != null)
            {
                file = fileAttribute.Value.Substring(0);
                inline = null;
            }
            else
            {
                file = null;
                inline = sourceElement.Value.Substring(0);
            }
        }

        #region IElementContainer Members

        private Element sourceElement;

        public Element SourceElement
        {
            get { return sourceElement; }
        }

        public void Commit()
        {
            sourceElement = new Element("script");

            if (!String.IsNullOrEmpty(file))
            {
                Attribute fileAttribute = new Attribute();
                fileAttribute.Name = "file";
                fileAttribute.Value = file;

                sourceElement.Attributes.Add(fileAttribute);
            }
            else
            {
                sourceElement.Value = inline;
            }
        }

        #endregion
    }
}
